package com.stablesoft.gamestat.data.wot;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.format.Time;

public class DBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "wot_statistic";

    // Tables
    private static final String TABLE_PLAYER = "players";
    private static final String TABLE_VEHICLE = "vehicles";
    private static final String TABLE_CLAN = "clans";
    private static final String TABLE_SNAPSHOT = "snapshots";
    private static final String TABLE_SNAPSHOT_VEHICLES = "snapshots_vehicles";
    
    // Columns
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_LOCALIZED_NAME = "localized_name";
    private static final String KEY_CREATED_DATE = "created_date";
    private static final String KEY_CLAN_ID = "clan_id";
    private static final String KEY_LEVEL = "level";
    private static final String KEY_NATION = "nation";
    private static final String KEY_IMAGE_URL = "image_url";
    private static final String KEY_CLASS = "class";
    private static final String KEY_DATE = "date";
    private static final String KEY_BATTLES = "battles";
    private static final String KEY_WINS = "wins";
    private static final String KEY_LOSSES = "losses";
    private static final String KEY_SURVIVED = "survived";    
    private static final String KEY_SPOTTED = "spotted";
    private static final String KEY_HITS = "hits_percent";
    private static final String KEY_DAMAGE = "damage_dealt";
    private static final String KEY_FRAGS = "frags";
    private static final String KEY_CAPTURE_POINTS = "capture_points";
    private static final String KEY_DEFENCE_POINTS = "defence_points";
    private static final String KEY_SNAPSHOT_ID = "snaphot_id";
    private static final String KEY_VEHICLE_ID = "vehicle_id";
    
    
	public DBHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_PLAYER_TABLE = "CREATE TABLE " + TABLE_PLAYER + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
				+ KEY_NAME + " TEXT,"
                + KEY_CREATED_DATE + " INTEGER" 
                + KEY_CLAN_ID + " INTEGER" + ")";
		
		String CREATE_VEHICLE_TABLE = "CREATE TABLE " + TABLE_VEHICLE + "("
                + KEY_ID + " INTEGER AUTOINCREMENT PRIMARY KEY,"
				+ KEY_NAME + " TEXT,"
				+ KEY_LOCALIZED_NAME + " TEXT,"
                + KEY_LEVEL + " INTEGER" 
                + KEY_NATION + " TEXT"
                + KEY_IMAGE_URL + " TEXT"
                + KEY_CLASS + " TEXT" + ")";
		
		String CREATE_CLAN_TABLE = "CREATE TABLE " + TABLE_CLAN + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
				+ KEY_NAME + " TEXT,"
				+ KEY_LOCALIZED_NAME + " TEXT" + ")";
		
		String CREATE_SNAPSHOT_TABLE = "CREATE TABLE " + TABLE_SNAPSHOT + "("
                + KEY_ID + " INTEGER AUTOINCREMENT PRIMARY KEY,"
				+ KEY_DATE + " INTEGER,"
				+ KEY_CLAN_ID + " INTEGER,"
                + KEY_BATTLES + " INTEGER" 
                + KEY_WINS + " INTEGER" 
                + KEY_LOSSES + " INTEGER" 
                + KEY_SURVIVED + " INTEGER" 
                + KEY_SPOTTED + " INTEGER" 
                + KEY_HITS + " FLOAT" 
                + KEY_DAMAGE + " INTEGER" 
                + KEY_FRAGS + " INTEGER" 
                + KEY_CAPTURE_POINTS + " INTEGER" 
                + KEY_DEFENCE_POINTS + " INTEGER" + ")";
		
		String CREATE_SNAPSHOT_VEHICLES = "CREATE TABLE " + TABLE_SNAPSHOT_VEHICLES + "("
                + KEY_ID + " INTEGER AUTOINCREMENT PRIMARY KEY,"
				+ KEY_SNAPSHOT_ID + " INTEGER,"
				+ KEY_VEHICLE_ID + " INTEGER,"
                + KEY_BATTLES + " INTEGER" 
                + KEY_WINS + " INTEGER" + ")";
		
        db.execSQL(CREATE_PLAYER_TABLE);
        db.execSQL(CREATE_VEHICLE_TABLE);
        db.execSQL(CREATE_CLAN_TABLE);
        db.execSQL(CREATE_SNAPSHOT_TABLE);
        db.execSQL(CREATE_SNAPSHOT_VEHICLES);
	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//TODO: Don't erase all data. Convert it instead...
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAYER);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_VEHICLE);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CLAN);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_SNAPSHOT);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_SNAPSHOT_VEHICLES);

		onCreate(db);
	}


	public Time getLastUpdate() {
		
		return null;
	}
}
